import { queryTempList, queryApplyForm, saveApplyForm, queryApprove } from '../services';

export default {
    namespace: 'workFlowTempList',

    state: {
        templateList: [],
        formList: [],
        approveList: [],
        explanation: undefined
    },
    effects: {
        *fetchList({ payload, callback }, { call, put }) {
            const response = yield call(queryTempList, payload);
            yield put({
                type: 'save',
                payload: {
                    templateList: response.data
                }
            });
            if (callback) {
                callback(response);
            }
        },
        *fetchFormList({ payload, callback }, { call, put }) {
            const response = yield call(queryApplyForm, payload);
            yield put({
                type: 'save',
                payload: {
                    formList: response.data.formList,
                    explanation: response.data.explanation
                }
            });
            if (callback) {
                callback(response);
            }
        },
        *updataApplyForm({ payload, callback }, { call }) {
            const response = yield call(saveApplyForm, payload);
            if (callback) {
                callback(response);
            }
        },
        *fetchApprove({ payload, callback }, { call, put }) {
            const response = yield call(queryApprove, payload);
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        approveList: response.data
                    }
                });
            } else {
                yield put({
                    type: 'save',
                    payload: {
                        approveList: []
                    }
                });
            }
            if (callback) {
                callback(response);
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            console.log('payload', payload);
            return {
                ...state,
                ...payload
            };
        }
    }
};
